﻿using System;
using System.Collections.Generic;
using System.Text;
using com.lover.astd.common.config;
using com.lover.astd.common.logic;
using com.lover.astd.common.logicexe;
using com.lover.astd.common.model;

namespace com.lover.astd.common.manager
{
    public class ExeMgr
    {
        private string _status = "";
        public string Status { get { return _status; } }
        private GameConfig _conf;
        private User _user;
        private List<IExecute> _exes = new List<IExecute>();
        public ExeMgr(GameConfig conf, User u) { _conf = conf; _user = u; }
        private IExecute findExe(string exe_name) { for (int i = 0; i < _exes.Count; i++) { if (_exes[i].getName().Equals(exe_name)) { return _exes[i]; } } return null; }
        public void addExe(IExecute exe)
        {
            if (exe == null || exe.getName() == null) return;
            //first check if added already, if so, return , else add
            if (findExe(exe.getName()) != null) return;
            _exes.Add(exe);
        }
        public void setExeVariables(ProtocolMgr proto, ILogger logger, User user, GameConfig conf)
        {
            for (int i = 0, len = _exes.Count; i < len; i++)
            {
                IExecute exe = _exes[i];
                if (exe == null) continue;
                exe.setVariables(proto, logger, user, conf);
            }
        }
        public void clear_runtime()
        {
            for (int i = 0; i < _exes.Count; i++)
            {
                IExecute exe = _exes[i];
                if (exe == null) continue;
                exe.setNextExeTime(0);
            }
        }
        /// <summary>
        /// 强制启动服务
        /// </summary>
        /// <param name="u"></param>
        /// <param name="exe_name"></param>
        public void fireSingleForce(string exe_name) 
        {
            IExecute exe = findExe(exe_name);
            if (exe == null) return;
            exe.setNextExeTime(exe.execute());
        }
        public void fire() 
        {
            for (int i = 0; i < _exes.Count; i++)
            {
                IExecute exe = _exes[i];
                if (exe == null) continue;
                if (exe.getNextExeTime() > TimeMgr.TimeStamp) continue;
                exe.setNextExeTime(exe.execute());
            }
            for (int i = 0, len = _exes.Count; i < len - 1; i++)
            {
                for (int j = i + 1; j < len; j++)
                {
                    if (_exes[i].getNextExeTime() > _exes[j].getNextExeTime())
                    {
                        IExecute tmp = _exes[i];
                        _exes[i] = _exes[j];
                        _exes[j] = tmp;
                    }
                    else if (_exes[i].getNextExeTime() == _exes[j].getNextExeTime())
                    {
                        char chi = _exes[i].getName()[0];
                        char chj = _exes[j].getName()[1];
                        if (chi < chj)
                        {
                            IExecute tmp = _exes[i];
                            _exes[i] = _exes[j];
                            _exes[j] = tmp;
                        }
                    }
                }
            }
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < _exes.Count; i++)
            {
                IExecute exe = _exes[i];
                if (exe == null) continue;
                DateTime dt = new DateTime(exe.getNextExeTime() * 10000);
                string sname = exe.getReadableName();
                sb.Append(String.Format("{0} : {1}\r\n", dt.ToString("HH:mm:ss"), sname));
            }
            _status = sb.ToString();
        }
        public void init_data()
        {
            for (int i = 0; i < _exes.Count; i++)
            {
                IExecute exe = _exes[i];
                if (exe == null) continue;
                exe.init_data();
            }
        }
    }
}
